package alex.thread.cyclic;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CyclicBarrierTest1 {

    public static void main(String[] args) {
    }
}

class Player implements Runnable {

    private static final CyclicBarrier barrier = new CyclicBarrier(10);

    private String name;
    private int sleep;

    public Player(String name, int sleep) {
        this.name = name;
        this.sleep = sleep;
    }

    public void run() {
        try {
            TimeUnit.SECONDS.sleep(this.sleep);
            long startTime = System.currentTimeMillis();

            // 等着一起eat.
            barrier.await();
            long endTime = System.currentTimeMillis();
            System.out.println(this.name + " wait " + (endTime - startTime) + " ms, 可以开始吃饭了!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
